// 将箭头函数转换成普通函数
const babel = require('@babel/core');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;

// 要转换的代码
const code = `
const add = (a, b) => a + b;
const square = x => {
    return x * x;
};
`;

// 解析代码为 AST
const ast = parser.parse(code);

// 定义 visitor 对象
const visitor = {
    ArrowFunctionExpression(path) {
        const { node } = path;
        const params = node.params.map(param => param.name).join(', ');
        const body = node.body.type === 'BlockStatement' ? node.body : {
            type: 'BlockStatement',
            body: [{
                type: 'ReturnStatement',
                argument: node.body
            }]
        };

        const functionDeclaration = {
            type: 'FunctionDeclaration',
            id: path.parent.id,
            params: node.params,
            body: body,
            async: node.async
        };

        path.replaceWith(functionDeclaration);
    }
};

// 遍历 AST
traverse(ast, visitor);

// 将修改后的 AST 转换回代码
const { code: transformedCode } = generate(ast);

// 输出转换后的代码
console.log(transformedCode);



// const add = function(a, b) {
//     return a + b;
// };
// const square = function(x) {
//     return x * x;
// };